<?php
require_once 'includes/header.php';
require_once 'includes/functions.php';
require_once 'includes/DatabaseHelper.php';
require_once '../includes/CognitiveModuleManager.php';

// 初始化认知模块管理器
$moduleManager = new CognitiveModuleManager();

// 确保传入了模块ID
if (!isset($_GET['module_id'])) {
    $_SESSION['error_message'] = '缺少模块ID参数';
    header('Location: cognitive_modules.php');
    exit;
}

$moduleId = intval($_GET['module_id']);
$module = $moduleManager->getModuleById($moduleId);

if (!$module) {
    $_SESSION['error_message'] = '找不到指定的认知模块';
    header('Location: cognitive_modules.php');
    exit;
}

// 处理删除内容请求
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['content_id'])) {
    $contentId = intval($_GET['content_id']);
    if ($moduleManager->deleteContent($contentId)) {
        $_SESSION['success_message'] = '内容删除成功';
    } else {
        $_SESSION['error_message'] = '内容删除失败';
    }
    header('Location: cognitive_contents.php?module_id=' . $moduleId);
    exit;
}

// 获取模块下的所有内容
$contents = $moduleManager->getModuleContents($moduleId);

// 获取知识百科
$encyclopediaEntries = $moduleManager->getEncyclopediaEntries($moduleId);
?>

<h1 class="mb-4"><i class="bi bi-card-list"></i> 认知内容管理</h1>

<!-- 面包屑导航 -->
<nav aria-label="breadcrumb" class="mb-4">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="cognitive_modules.php?age_group=<?php echo urlencode($module['age_group']); ?>"><?php echo $module['age_group']; ?>岁认知模块</a></li>
        <li class="breadcrumb-item active" aria-current="page"><?php echo htmlspecialchars($module['module_name']); ?></li>
    </ol>
</nav>

<!-- 操作区域 -->
<div class="card mb-4">
    <div class="card-body">
        <div class="row">
            <div class="col-md-7">
                <h5><?php echo htmlspecialchars($module['module_name']); ?> <span class="badge" style="background-color: <?php echo htmlspecialchars($module['background_color']); ?>; color: #fff;"><?php echo $module['age_group']; ?>岁</span></h5>
                <p class="text-muted"><?php echo htmlspecialchars($module['description']); ?></p>
            </div>
            <div class="col-md-5 text-end">
                <a href="cognitive_content_edit.php?module_id=<?php echo $moduleId; ?>" class="btn btn-primary">
                    <i class="bi bi-plus-circle"></i> 添加内容
                </a>
                <a href="cognitive_encyclopedia_edit.php?module_id=<?php echo $moduleId; ?>" class="btn btn-outline-primary ms-2">
                    <i class="bi bi-book"></i> 添加百科内容
                </a>
                <a href="cognitive_module_edit.php?id=<?php echo $moduleId; ?>" class="btn btn-outline-secondary ms-2">
                    <i class="bi bi-pencil"></i> 编辑模块
                </a>
            </div>
        </div>
    </div>
</div>

<!-- 消息提示 -->
<?php if (isset($_SESSION['success_message'])): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['success_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['success_message']); ?>
<?php endif; ?>

<?php if (isset($_SESSION['error_message'])): ?>
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['error_message']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
    <?php unset($_SESSION['error_message']); ?>
<?php endif; ?>

<!-- 内容列表 -->
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-list-ul me-1"></i> 内容列表
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>内容标题</th>
                        <th>标识符</th>
                        <th>颜色/特性</th>
                        <th>顺序</th>
                        <th>示例数</th>
                        <th>状态</th>
                        <th width="150">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($contents) > 0): ?>
                        <?php foreach ($contents as $content): ?>
                            <?php 
                            // 获取示例数量
                            $examples = $moduleManager->getContentExamples($content['content_id']);
                            $examplesCount = count($examples);
                            ?>
                            <tr>
                                <td><?php echo $content['content_id']; ?></td>
                                <td>
                                    <div style="display: flex; align-items: center;">
                                        <?php if (!empty($content['color_code'])): ?>
                                            <div class="color-sample me-2" style="background-color: <?php echo htmlspecialchars($content['color_code']); ?>"></div>
                                        <?php endif; ?>
                                        <?php echo htmlspecialchars($content['content_title']); ?>
                                    </div>
                                </td>
                                <td><?php echo htmlspecialchars($content['content_key']); ?></td>
                                <td>
                                    <?php if (!empty($content['color_code'])): ?>
                                        <span class="badge" style="background-color: <?php echo htmlspecialchars($content['color_code']); ?>">
                                            <?php echo htmlspecialchars($content['color_code']); ?>
                                        </span>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $content['content_order']; ?></td>
                                <td><?php echo $examplesCount; ?></td>
                                <td>
                                    <?php if ($content['is_active']): ?>
                                        <span class="badge bg-success">显示</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">隐藏</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="cognitive_content_edit.php?id=<?php echo $content['content_id']; ?>" class="btn btn-sm btn-primary">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="cognitive_examples.php?content_id=<?php echo $content['content_id']; ?>" class="btn btn-sm btn-info">
                                        <i class="bi bi-images"></i>
                                    </a>
                                    <a href="cognitive_contents.php?action=delete&content_id=<?php echo $content['content_id']; ?>&module_id=<?php echo $moduleId; ?>" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除此内容吗？这将同时删除内容下的所有示例！')">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="8" class="text-center">暂无内容</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 知识百科列表 -->
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-book me-1"></i> 知识百科
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>标题</th>
                        <th>图片</th>
                        <th>显示顺序</th>
                        <th>状态</th>
                        <th width="150">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($encyclopediaEntries) > 0): ?>
                        <?php foreach ($encyclopediaEntries as $entry): ?>
                            <tr>
                                <td><?php echo $entry['encyclopedia_id']; ?></td>
                                <td><?php echo htmlspecialchars($entry['title']); ?></td>
                                <td>
                                    <?php if (!empty($entry['image_path'])): ?>
                                        <img src="<?php echo htmlspecialchars($entry['image_path']); ?>" style="height: 50px; width: auto;" class="img-thumbnail">
                                    <?php else: ?>
                                        <span class="text-muted">无图片</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $entry['encyclopedia_order']; ?></td>
                                <td>
                                    <?php if ($entry['is_active']): ?>
                                        <span class="badge bg-success">显示</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">隐藏</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <a href="cognitive_encyclopedia_edit.php?id=<?php echo $entry['encyclopedia_id']; ?>" class="btn btn-sm btn-primary">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="cognitive_contents.php?action=delete_encyclopedia&id=<?php echo $entry['encyclopedia_id']; ?>&module_id=<?php echo $moduleId; ?>" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除此百科条目吗？')">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" class="text-center">暂无百科内容</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 静态页面预览 -->
<div class="card mb-4">
    <div class="card-header">
        <i class="bi bi-eye me-1"></i> 页面预览
    </div>
    <div class="card-body">
        <p>您可以随时查看生成的静态页面效果：</p>
        <a href="../age-<?php echo $module['age_group']; ?>-<?php echo $module['module_key']; ?>.html" class="btn btn-outline-primary" target="_blank">
            <i class="bi bi-box-arrow-up-right"></i> 预览页面
        </a>
        <button class="btn btn-outline-success ms-2" id="generatePageBtn">
            <i class="bi bi-file-earmark-code"></i> 生成/更新静态页面
        </button>
    </div>
</div>

<style>
.color-sample {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 1px solid #ddd;
}
</style>

<?php require_once 'includes/footer.php'; ?> 